<template>
	<view>
		<view v-if="isShow" class="control">
			<view class="line-item">
				<text>当前元素名称：{{hotPoint.name}}</text>
			</view>
			<view class="line-item">
				<text>Scale X :</text>
				<slider class="flex-1" @changing="changeInfo('sx',$event)" :min="0" :max="10" :step="0.05"
					:value="info.sx" :block-size="12" show-value />
			</view>
			<view class="line-item">
				<text>Scale Y :</text>
				<slider class="flex-1" @changing="changeInfo('sy',$event)" :min="0" :max="10" :step="0.05"
					:value="info.sy" :block-size="12" show-value />
			</view>
			<view class="line-item">
				<text>Scale Z :</text>
				<slider class="flex-1" @changing="changeInfo('sz',$event)" :min="0" :max="10" :step="0.05"
					:value="info.sz" :block-size="12" show-value />
			</view>

			<view class="line-item">
				<text>Position X :</text>
				<slider class="flex-1" @changing="changeInfo('px',$event)" :min="-10" :max="10" :step="0.05"
					:value="info.px" :block-size="12" show-value />
			</view>
			<view class="line-item">
				<text>Position Y :</text>
				<slider class="flex-1" @changing="changeInfo('py',$event)" :min="-10" :max="10" :step="0.05"
					:value="info.py" :block-size="12" show-value />
			</view>
			<view class="line-item">
				<text>Position Z :</text>
				<slider class="flex-1" @changing="changeInfo('pz',$event)" :min="-10" :max="10" :step="0.05"
					:value="info.pz" :block-size="12" show-value />
			</view>
			<view class="buttons">
				<button type="primary" size="mini" @click="exportJSON">生成JSON</button>
				<button type="warn" size="mini" @click="deleteItem">删除元素</button>
			</view>
		</view>

		<view class="hot-img-button" @click="showImageControl">
			热点图配置
		</view>

		<uni-popup ref="popup" type="bottom">
			<view class="img-control">
				<image :src="hotImageUrl" />
				<button type="default" @click="changeHotImageUrl(1)">下一个</button>
				<button type="default" @click="changeHotImageUrl(-1)">上一个</button>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import assetsConfig from '../assetsConfig.js';

	export default {
		props: {
			hotPoint: {
				type: Object,
				default: function() {
					return null;
				}
			}
		},
		data() {
			return {
				isShow: false,
				info: {
					px: 0,
					py: 0,
					pz: 0,
					sx: 0,
					sy: 0,
					sz: 0,
				},
				hotImageUrl: ''
			}
		},
		mounted() {
			this.hotImageUrl = assetsConfig.hotPointRes[0];
		},
		watch: {
			hotPoint: {
				deep: true,
				handler(newVal, oldVal) {
					if (this.isShow && (newVal && oldVal && newVal.name != oldVal.name)) {
						this.getinfo();
					} else if (!this.isShow && newVal) {
						this.getinfo();
						this.isShow = true;
					} else if (!newVal) {
						this.isShow = false;
					}
				}
			},
			info: {
				deep: true,
				handler(newVal, oldVal) {
					let {
						px,
						py,
						pz,
						sx,
						sy,
						sz
					} = this.info
					this.hotPoint.scale.set(sx, sy, sz);
					this.hotPoint.position.set(px, py, pz);
				}
			}
		},
		methods: {
			getinfo() {
				let {
					position,
					scale
				} = this.hotPoint;

				this.info = {
					px: position.x,
					py: position.y,
					pz: position.z,
					sx: scale.x,
					sy: scale.y,
					sz: scale.z,
				}
			},
			changeInfo(key, e) {
				this.info[key] = e.target.value;
			},
			exportJSON() {
				this.$emit('exportJSON');
			},
			deleteItem() {
				this.$emit('deleteItem', this.hotPoint);
			},
			showImageControl() {
				this.$refs.popup.open();
			},
			changeHotImageUrl(n) {
				let hotPointRes = assetsConfig.hotPointRes;
				let findIndex = hotPointRes.findIndex(item => item == this.hotImageUrl);
				let nextIndex = findIndex + n;
				if (nextIndex >= hotPointRes.length) {
					nextIndex = 0;
				} else if (nextIndex < 0) {
					nextIndex = hotPointRes.length - 1;
				}
				this.hotImageUrl = hotPointRes[nextIndex];
			}
		}
	}
</script>

<style scoped lang="scss">
	.control {
		position: absolute;
		background-color: rgba(0, 0, 0, .4);
		padding: 15rpx;
		left: 0;
		top: 0;
		width: 100%;
	}

	.line-item {
		display: flex;
		align-items: center;
		color: #fff;
		font-size: 26rpx;
		justify-content: center;

		text {
			margin-right: 10rpx;
		}

		.flex-1 {
			flex: 1;
		}

	}

	.buttons {
		display: flex;
		justify-content: space-between;
		padding: 0 30rpx;
		margin-top: 10rpx;
	}

	.img-control {
		background-color: rgba(0, 0, 0, .4);
		padding: 30rpx 30rpx 60rpx 30rpx;
		width: 100%;
		display: flex;
		align-items: center;

		image {
			width: 100rpx;
			height: 100rpx;
		}

		button {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 130rpx;
			height: 50rpx;
			font-size: 18rpx;
			margin-right: 0;
		}

		*+* {
			margin-left: 20rpx;
		}

	}

	.hot-img-button {
		position: absolute;
		left: 50%;
		transform: translateX(-50%);
		bottom: 0;
		background-color: rgba(0, 0, 0, .6);
		border-radius: 8rpx 8rpx 0 0;
		color: #fff;
		padding: 10rpx 30rpx;
	}

	/deep/ uni-slider .uni-slider-value {
		color: #fff !important;
	}
</style>
